<template>
	<view>
		<user-list @changeData="changeData" :friendData="friendData"></user-list>
		<view class="bottom-btn-zw"></view>
		<view class="bottom-btn">
			<view>
				<button @click="addUserGroup">拉取</button>
			</view>
		</view>
	</view>
</template>

<script>
	import userList from './child/user-list.vue'

	import {
		getGroupFriend,
		queryGroupByHeadInfo
	} from '@/api/http/msg/group.js'

	export default {
		components: {
			userList
		},
		data() {
			return {
				uid: uni.getStorageSync("userId"),
				addDataList: [],
				groupData: {},
				friendData: [],
				inviteType: '',
				groupInfo: {}
			}
		},
		methods: {
			queryGroupByHeadInfo() {
				queryGroupByHeadInfo(this.groupData.id, this.uid).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					this.inviteType = res.data.busInfo.inviteType
					this.groupInfo = res.data.busInfo
					//busInfo.identity != '1' && busInfo.identity != '2'
					//console.log(res.data.busInfo)
				})
			},
			getFriendData() {
				getGroupFriend(this.uid, this.groupData.id).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					this.friendData = res.data.busInfo
				})
			},
			changeData(addData) {
				//console.log(addData)
				this.addDataList = addData
			},
			addUserGroup() {
				//身份判断
				if (this.inviteType == '1') {
					//邀请验证方式
					if (this.groupInfo.identity != '1' && this.groupInfo.identity != '2') {
						return uni.$showMsg('您无权拉取用户，请联系管理员！');
					}
				}

				var userIds = ''
				for (var i = 0; i < this.addDataList.length; i++) {
					userIds += this.addDataList[i].userId
					userIds += ','
				}
				var addIds = userIds.substring(0, userIds.length - 1)
				var reqData = {
					type: this.$chatType.PULL_FRIEND,
					msg: {
						senderId: uni.getStorageSync('userId'),
						senderName: uni.getStorageSync('nickname'),
						senderHead: uni.getStorageSync('head'),
						message: '邀请用户加入群聊：' + this.groupData.nickname,
						addId: addIds,
						sysType: this.groupData.type,
						groupId: this.groupData.id,
						groupHead: this.groupData.head,
						groupName: this.groupData.nickname,
						types: 101,
						//receiveId:
					}
				}
				//console.log(reqData)
				uni.myWebsocket.socketTask.send({
					data: JSON.stringify(reqData),
					async success() {
						//console.log('发送成功')
						uni.$showMsg('消息发送成功！');
						setTimeout(() => {
							//返回上一页
							return uni.navigateBack(1)
						}, 100)

					},
				});

			}
		},
		onLoad(e) {
			var res = JSON.parse(e.groupData)
			this.groupData = res
			//console.log(res)
			this.getFriendData()
			this.queryGroupByHeadInfo()
		}

	}
</script>

<style lang="scss" scoped>
	.bottom-btn-zw{
		width: 100%;
		height: 120rpx;
	}
	
	.bottom-btn {
		width: 100%;
		height: 120rpx;
		position: fixed;
		z-index: 9;
		bottom: 0;
		left: 0;
		background-color: #fff;

		view {
			button {
				width: 50%;
				height: 80rpx;
				border-radius: 50rpx;
				font-size: 34rpx;
				line-height: 80rpx;
				margin-top: 20rpx;
				background: rgba(247, 74, 115, 1);
				color: #fff;
			}
		}
	}
</style>